package com.lgkj.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lgkj.R;
import com.lgkj.bean.meitao.CarGoodsBean;
import com.lgkj.utils.CommonUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/8/20 0020.
 */

public class CarAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;

    public List<CarGoodsBean.DataBean> getCarGoodsBean() {
        return carGoodsBean;
    }

    private List<CarGoodsBean.DataBean> carGoodsBean;

    public List<String> getCarId() {
        return carId;
    }

    private List<String> carId;


    public List<CarGoodsBean.DataBean> getTotalPrices() {
        return totalPrices;
    }

    public void setTotalPrices(List<CarGoodsBean.DataBean> totalPrices) {
        this.totalPrices = totalPrices;
    }

    private List<CarGoodsBean.DataBean> totalPrices;
    private int type = 2;  //0  全选   1全不选    2自选

    public void setIsall(boolean isall) {
        this.isall = isall;
        this.type = isall ? 0 : 1;
        notifyDataSetChanged();
    }

    private boolean isall = true;

    public CarAdapter(Context context) {
        this.context = context;
        carId = new ArrayList<>();
        carGoodsBean = new ArrayList<>();
        totalPrices = new ArrayList<>();
    }

    private CallBack callBack;

    public void setCallBack(CallBack back) {
        this.callBack = back;
    }


    public void setCarGoodsBean(List<CarGoodsBean.DataBean> dataBean) {
        this.carGoodsBean = dataBean;
    }

    @Override
    public int getCount() {
        return carGoodsBean.size();
    }

    @Override
    public CarGoodsBean.DataBean getItem(int i) {
        return carGoodsBean.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_car, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.tvPrice.setText(carGoodsBean.get(i).getGoods_price() + "");
        viewHolder.tvName.setText(carGoodsBean.get(i).getGoods_name());
        viewHolder.etNum.setText(carGoodsBean.get(i).getGoods_num() + "");
        CommonUtils.LoadImage(context, viewHolder.ivImg, carGoodsBean.get(i).getCover_id(), 0);
        if (type == 0) {
            carGoodsBean.get(i).setAdd(true);
        } else if (type == 1) {
            carGoodsBean.get(i).setAdd(false);
        }
        viewHolder.cbCar.setChecked(carGoodsBean.get(i).isAdd());
        if (carGoodsBean.get(i).isAdd()) {
            carId.add(carGoodsBean.get(i).getId());
            BigDecimal bDouble = new BigDecimal(carGoodsBean.get(i).getGoods_num());
            BigDecimal bString = new BigDecimal(carGoodsBean.get(i).getGoods_price());
            BigDecimal num = bDouble.multiply(bString);
            carGoodsBean.get(i).setTotalPrice(num);
            totalPrices.addAll(carGoodsBean);
            Log.i("getView: ", num + "");
        } else {
            carId.remove(carGoodsBean.get(i).getId());
        }
//        Log.i("onClick: ", "i:" + i + carGoodsBean.get(i).isAdd() + "==" + viewHolder.cbCar.isChecked());
        viewHolder.cbCar.setOnClickListener(this);
        viewHolder.cbCar.setTag(i);
        viewHolder.ivJian.setOnClickListener(this);
        viewHolder.ivJian.setTag(i);
        viewHolder.ivAdd.setOnClickListener(this);
        viewHolder.ivAdd.setTag(i);
        return view;
    }

    @Override
    public void onClick(View view) {
        int position = Integer.parseInt(view.getTag().toString());
        switch (view.getId()) {
            case R.id.iv_jian:
                callBack.clickJian(position);
                break;
            case R.id.iv_add:
                callBack.clickAdd(position);
                break;
            case R.id.cb_car:
                type = 2;
                CheckBox checkBox = (CheckBox) view;
                carGoodsBean.get(position).setAdd(checkBox.isChecked());
                notifyDataSetChanged();
                callBack.clickcb(position);
                break;
        }
    }

    static class ViewHolder {
        @BindView(R.id.cb_car)
        CheckBox cbCar;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.iv_img)
        ImageView ivImg;
        @BindView(R.id.iv_jian)
        ImageView ivJian;
        @BindView(R.id.et_num)
        EditText etNum;
        @BindView(R.id.iv_add)
        ImageView ivAdd;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface CallBack {
        void clickJian(int i);

        void clickAdd(int i);

        void clickcb(int i);
    }

}
